package com.xbs.controller.enterprise;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.xbs.controller.base.BaseServlet;
import com.xbs.entity.CarModelEntity;
import com.xbs.entity.EnterpriseModelEntity;
import com.xbs.entity.EnterpriseOrderDetailEntity;
import com.xbs.entity.EnterpriseOrderEntity;
import com.xbs.service.enterprise.EnterpriseIndexService;
import com.xbs.utils.EnterpriseUtils;

/**
 * 处理与企业订单相关的请求
 * 
 * @author Administrator
 *
 */
@WebServlet("*.order")
public class EnterpriseOrderPayController extends BaseServlet {
	private EnterpriseUtils eutil = new EnterpriseUtils();
	private EnterpriseIndexService service = new EnterpriseIndexService();

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		/**
		 * 通过获取请求的servlet路径,然后通过反射调用相关方法
		 */
		// 获取servlterd的路径
		String servletPath = req.getServletPath();
		// 获取方法名称
		String methodName = servletPath.substring(12, servletPath.length() - 6);
		try {
			// 获取方法
			Method method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class,
					HttpServletResponse.class);
			method.invoke(this, req, resp);
		} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
				| InvocationTargetException e) {
			e.printStackTrace();

		}

	}

	/**
	 * 处理自选订单请求
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void oneselfOption(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		/**
		 * 填充订单实体对象相关数据 EnterpriseOrderEntity
		 */
		resp.setCharacterEncoding("utf-8");
		String account=(String) req.getSession().getAttribute("account");
		String orderId = eutil.getEnterpriseAccount(account);
		Date bookTime = (Date) req.getSession().getAttribute("bookTime");
		EnterpriseOrderEntity eoe = new EnterpriseOrderEntity();
		eoe.setEo_account(account);
		eoe.setEo_order_id(orderId);
		eoe.setEo_order_status("已预约");
		eoe.setEo_reserve_get_time(bookTime);
		
		EnterpriseUtils util=new EnterpriseUtils();
		String xiadan=util.getDateTime();
		eoe.setEo_establish_time(Timestamp.valueOf(xiadan));
		/**
		 * 保存EnterpriseOrderEntity对象到数据库
		 */
		service.saveEnterpriseOrder(eoe);
		/**
		 * 创建一个list企业车型集合对象
		 */
		List<EnterpriseModelEntity> models = new ArrayList<EnterpriseModelEntity>();

		String[] carids = (String[]) req.getSession().getAttribute("carids");
		String[] carnums = (String[]) req.getSession().getAttribute("carnums");
		for (int i = 0; i < carids.length; i++) {
			EnterpriseModelEntity enterpriseModel = new EnterpriseModelEntity();
			enterpriseModel.setCar_model_id(carids[i]);
			enterpriseModel.setCar_model_number(Integer.parseInt(carnums[i]));
			enterpriseModel.setEo_order_id(orderId);
			enterpriseModel.setRent_money(service.getEnterpriseRent(carids[i]));
			models.add(enterpriseModel);
		}
		service.saveEnterpriseModels(models);

		forward("businessview/ls_enterprise/enterprisePayOrder");
	}

	public void confirmOneselfOption(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		/**
		 * 获取相关的值
		 */
		Date bookTime = Date.valueOf(req.getParameter("bookTime"));
		String orderId = eutil.getEnterpriseAccount("fykj");
		String[] carids = req.getParameterValues("carid");
		
		String[] carnums = req.getParameterValues("carnum");
		List<EnterpriseOrderDetailEntity> carmodels = service.getCarModelByIds(carids, carnums);
		double sum=0.0;
		for(EnterpriseOrderDetailEntity en:carmodels){
			sum+=(double)(en.getNum())*en.getRentCash();
		}
		/**
		 * 把值设置到request请求域
		 */
		req.getSession().setAttribute("bookTime", bookTime);
		req.setAttribute("orderId", orderId);
		req.getSession().setAttribute("carids", carids);
		req.getSession().setAttribute("carmodels", carmodels);
		req.getSession().setAttribute("carnums", carnums);
		req.setAttribute("sum", sum);
		forward("businessview/ls_enterprise/enterprisePayOrder");
	}

	/**
	 * 显示待确定企业车队订单
	 * @throws IOException 
	 * @throws ServletException 
	 */
	public void waitConfirmMotorcade(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		String motoid=req.getParameter("motoid");
		List<EnterpriseOrderDetailEntity> motocarmodels=service.getCarmotoById(motoid);
		req.getSession().setAttribute("motocarmodels", motocarmodels);
		String bookTime=req.getParameter("bookTime");
		req.getSession().setAttribute("bookTime", bookTime);
		req.setAttribute("prise", req.getParameter("motoprise"));
		forward("businessview/ls_enterprise/enterpriseMotoOrder");	
	}
	/**
	 * 提交企业车队订单
	 */
	public void submitmotoOrder(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String orderId = eutil.getEnterpriseAccount("fykj");
		Date bookTime = Date.valueOf((String) req.getSession().getAttribute("bookTime"));
		EnterpriseOrderEntity eoe = new EnterpriseOrderEntity();
		eoe.setEo_account("fykj");
		eoe.setEo_order_id(orderId);
		eoe.setEo_order_status("已预约");
		eoe.setEo_reserve_get_time(bookTime);
		eoe.setEo_rent_type(1);

		
		eoe.setEo_establish_time(Timestamp.valueOf(eutil.getDateTime()));
		/**
		 * 保存EnterpriseOrderEntity对象到数据库
		 */
		
		/**
		 * 往企业车型表存入数据
		 */
		double motosum=0.0;
		//获取车队信息
		List<EnterpriseOrderDetailEntity> motocarmodels=(List<EnterpriseOrderDetailEntity>) req.getSession().getAttribute("motocarmodels");
		List<EnterpriseModelEntity> models = new ArrayList<EnterpriseModelEntity>();
		System.out.println(motocarmodels.get(0).getRentCash());
		for(EnterpriseOrderDetailEntity e:motocarmodels){
			EnterpriseModelEntity o=new EnterpriseModelEntity();
			motosum+=e.getRentCash();
			o.setCar_model_id(e.getCarid());
			o.setCar_model_number(e.getNum());
			o.setEo_order_id(eoe.getEo_order_id());
			o.setRent_money(0.0);
			models.add(o);
		}
		eoe.setSumCash(motosum);
		service.saveEnterpriseOrder(eoe);
		service.saveEnterpriseModels(models);
		redirect("");
	}
	
}
